package top.dingwen.io.treasure.api.base.response;

import lombok.Getter;
import lombok.Setter;
import top.dingwen.io.treasure.api.base.constant.ApiConstant;

/**
 * SingleResponse : 统一出参单个对象
 *
 * @author dingwen
 * @since 2024/10/22 15:43
 */
@SuppressWarnings(ApiConstant.ALL)
@Getter
@Setter
public class SingleResponse<T> implements IBaseResponse {

    /**
     * 执行结果
     */
    private Boolean success;

    /**
     * 业务响应码
     */
    private String code;

    /**
     * 业务响应消息
     */
    private String message;


    /**
     * 数据对象
     */
    private T data;

    /**
     * 构建成功的单一结果
     *
     * @param data 数据
     * @return {@link SingleResponse } 单一结果
     */
    public static <T> SingleResponse<T> success(T data){
        SingleResponse<T> singleResponse = new SingleResponse<>();
        singleResponse.setSuccess(Boolean.TRUE);
        singleResponse.setData(data);
        return singleResponse;
    }

    /**
     * 构建单一的失败的单一结果
     *
     * @param code  业务编码
     * @param message 业务消息
     * @return {@link SingleResponse } 单一结果
     */
    public static <T> SingleResponse<T> failure(String code,String message){
        SingleResponse<T> singleResponse = new SingleResponse<>();
        singleResponse.setSuccess(Boolean.FALSE);
        singleResponse.setCode(code);
        singleResponse.setMessage(message);
        return singleResponse;
    }
}
